package sg.edu.nus.comp.qa.question.parser;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.List;
import java.util.Scanner;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import sg.edu.nus.comp.qa.question.ComparableQuestion;
import sg.edu.nus.comp.qa.question.Question;

public class QuestionListParser{
	private static Log LOGGER = LogFactory.getLog(QuestionListParser.class);
	
	public void loadFile(String filePath, List<ComparableQuestion> questionList){
		try {
			Scanner scanner = new Scanner(new File(filePath));
			while(scanner.hasNext()){
				String line = scanner.nextLine();
				String[] parts = line.split("	");
				if(parts.length==2)
					questionList.add(new ComparableQuestion(parts[0], parts[1]));
			}
			scanner.close();
		} catch (FileNotFoundException e) {
			LOGGER.error("File Not Found", e);
		}
	}
	
	public void writeFile(String filename, List<ComparableQuestion> questionList){
		StringBuilder output = new StringBuilder();
		for(Question question : questionList){
			output.append(question.getQuestion()).append('	').append(question.getAnswer()).append("\n");
		}
		try {
			Writer writer= new FileWriter(new File(filename), true);
			writer.write(output.toString());
			writer.close();
		} catch (IOException e) {
			LOGGER.error("IO Exceotion", e);
		}
		
	}

}
